package com.jsj.controller;

import com.jsj.pojo.Repair;
import com.jsj.pojo.RepairHistory;
import com.jsj.service.HistoryService;
import com.jsj.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class RepairController {

    @Autowired
    RepairService repairService;
    @Autowired
    HistoryService historyService;


//添加设备维修
    @RequestMapping("/addRepair")
    @ResponseBody
    public Boolean addRepair(Repair repair){
        System.out.println("--repair---"+repair.toString());
        Boolean flag = repairService.addRepair(repair);
        return flag;
    }
//默认全查
    @RequestMapping("/queryAllRepair")
    @ResponseBody
    public List<Repair> queryAllRepair(){
        List<Repair> repairsList = repairService.queryAllRepair();
        return repairsList;
    }
    //关键字搜索
    @RequestMapping("/queryRepairByName")
    @ResponseBody
    public List<Repair> queryRepairByName(String deviceName){
        List<Repair> repairsList = repairService.queryRepairByName(deviceName);
        return repairsList;
    }

    @RequestMapping("/updateRepairById")
    @ResponseBody
    public Boolean updateRepairById(Repair repair){
        Boolean flag = repairService.updateRepairById(repair);
        return flag;

    }
    //维修完毕，
    @RequestMapping("/repairedAndReturn")
    @ResponseBody
    public Boolean repairedAndReturn(int repairId, int deviceId, String returnTime) throws ParseException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date returnTime2 = format.parse(returnTime);
        Boolean flag = repairService.repairedAndReturn(repairId, deviceId, returnTime2);

        return flag;
    }
//未审核，已审核，已维修
    @RequestMapping("/queryRepairByStatus")
    @ResponseBody
    public List<Repair> queryRepairByStatus(String status){
        List<Repair> repairList = repairService.queryRepairByStatus(status);

        return repairList;
    }

//删除记录
    @RequestMapping("/delRepairById")
    @ResponseBody
    public Boolean delRepairById(RepairHistory repairHistory){
        Boolean flag = repairService.delRepairById(repairHistory.getRepairId());
        Boolean aBoolean = historyService.addRepairHistory(repairHistory);

        if (flag=true){
            if (aBoolean=true){
                return true;
            }
        }
        return false;
    }

    //撤销申请，并删除该条被拒绝的数据
    @RequestMapping("updateRepairDeviceStatus")
    @ResponseBody
    public Boolean updateRepairDeviceStatus(int repairId, int deviceId){

        Boolean aBoolean = repairService.updateRepairDeviceStatus(repairId, deviceId);
        return aBoolean;

    }


}
